import 'dart:convert';

import 'package:aichat_flutter_wallet/module/module_browser/provider/browser_proviser.dart';

import 'package:aichat_flutter_wallet/module/module_browser/viewmodel/before_browser.dart';

import 'package:aichat_flutter_wallet/module/module_browser/widget/common_widget.dart';


import 'package:aichat_flutter_wallet/module_common/provider/nav_key.dart';

import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/utils/sign.dart';

import 'package:aichat_flutter_wallet/module_common/widgets/button/public_button.dart';

import 'package:aichat_flutter_wallet/module_common/widgets/dialog/bottom_sheet_widget.dart';
import 'package:encrypt/encrypt.dart';

import 'package:flutter/material.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';

import 'package:lib_util/lib_util.dart';

class Signature extends StatefulWidget {
  String sign;

  Function? onConfrim;

  Signature({super.key, this.sign = '', this.onConfrim});

  @override
  State<StatefulWidget> createState() {
    return _SignatureState();
  }
}

class _SignatureState extends State<Signature> {
  WebSiteInfoItem? webSiteInfoItem =
      NavKey.getContext().read<BrowserProvider>().webSiteInfoItem;
  String message = '';
  String messageDecode = '';
  @override
  void initState() {
    super.initState();
    message = widget.sign;
    if (message.startsWith('0x')) {
      message = message.substring(2);
    }
    try {
      messageDecode = utf8.decode(decodeHexString(message));
    } catch (e) {
      Config.logger.e(e);
      messageDecode = message;
    }
  }

  signData() async {
    bool iscorrect = await Sign.verifyPwd();
    if (iscorrect) {
      Navigator.of(context).pop(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return BottomSheetWidget(
      title: 'Request Signature',
      expand: false,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.only(top: 12.w, bottom: 12.w),
              child: CommonWidgetUtil.websizeWidget(
                  image: webSiteInfoItem == null
                      ? ''
                      : webSiteInfoItem!.websiteImg,
                  title: webSiteInfoItem == null
                      ? ""
                      : webSiteInfoItem!.websiteName,
                  subTitle: webSiteInfoItem == null
                      ? ""
                      : webSiteInfoItem!.websiteUrl),
            ),
            SizedBox(
              height: 10.w,
            ),
            Text(
              'Signature Information',
              style: TextStyle(
                  fontSize: 14.sp,
                  color: AppColors.c_333333,
                  fontWeight: FontWeight.w500),
            ),
            SizedBox(
              height: 10.w,
            ),
            Container(
              height: 200.w,
              width: double.infinity,
              padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 15.w),
              decoration: BoxDecoration(
                  border: Border.all(width: 1.w, color: AppColors.c_F6F6F6),
                  borderRadius: BorderRadius.all(Radius.circular(10.w))),
              child: SingleChildScrollView(
                  child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [Text(widget.sign)],
              )),
            ),
            SizedBox(
              height: 10.w,
            ),
            Container(
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                      child: PublicButton(
                          margin: EdgeInsets.zero,
                          type: ButtonType.defaultType,
                          text: 'Cancel',
                          onClick: () {
                            Navigator.of(context).pop();
                          })),
                  SizedBox(
                    width: 20.w,
                  ),
                  Expanded(
                      child: PublicButton(
                    margin: EdgeInsets.zero,
                    type: ButtonType.private,
                    text: 'Comfirm',
                    onClick: signData,
                  ))
                ],
              ),
            ),
            SizedBox(
              height: 10.w + MediaQuery.of(context).padding.bottom.w,
            ),
          ],
        ),
      ),
    );
  }
}
